package com.lmk.system.controller;

import com.lmk.basic.util.AjaxResult;
import com.lmk.basic.util.PageList;
import com.lmk.system.domain.Dictionarydetail;
import com.lmk.system.query.DictionarydetailQuery;
import com.lmk.system.service.DictionarydetailService;
import com.lmk.system.service.DictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionarydetailController {
    @Autowired
    private DictionarydetailService service;

    @Autowired
    private DictionarytypeService dictionarytypeService;


    // 查询所有,使用get请求
    @GetMapping
    public List<Dictionarydetail> findAll(){
        List<Dictionarydetail> list = service.findAll();
        return list;
    }
    // 查询单个
    @GetMapping("/{id}")
    // @PathVariable("id")从路径上获取id
    public Dictionarydetail findOne(@PathVariable("id") Long id){
        Dictionarydetail byId = service.findById(id);
        return byId;
    }

    @GetMapping("/tepe/{id}")
    // @PathVariable("id")从路径上获取id
    public List<Dictionarydetail> findOnetepe(@PathVariable("id") Long id){

        return  dictionarytypeService.findDetail(id);
    }

    // 分页查询
    @PostMapping
    // @RequestBody 从请求体中获取数据
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery query){
        PageList<Dictionarydetail> list = service.queryPage(query);
        return list;
    }
    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            service.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败了");
        }
    }
    // 添加和修改
    @PutMapping
    public AjaxResult addorupdate(@RequestBody Dictionarydetail detail){
        try {
            if (detail.getId()==null){
                service.add(detail);
            }else {
                service.update(detail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }

    }

    @PatchMapping// 批量删除
    public AjaxResult batchdel(@RequestBody List<Long> ids){
        try {
            service.batchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }
}
